<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="预订人姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入预订人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号码" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="roomOrderList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="预订人姓名" align="center" prop="userName" />
      <el-table-column label="身份证号码" align="center" prop="idCard" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="入住时间" align="center" prop="liveTime" width="180">
      </el-table-column>
      <el-table-column label="离店时间" align="center" prop="leaveTime" width="180">
      </el-table-column>
      <el-table-column label="居住天数(天)" align="center" prop="dwellingDay" />
      <el-table-column label="订单状态" align="center" prop="statusName" />
      <el-table-column label="房间" align="center" prop="roomName" />
      <el-table-column label="订单价格(元)" align="center" prop="orderPrice" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-check"
            v-if="scope.row.status==1"
            @click="handleAudit(scope.row)"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.status!==3"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客房订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="入住时间" prop="liveTime">
          <el-date-picker clearable
                          v-model="form.liveTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          @change="getDay"
                          placeholder="请选择入住时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="离店时间" prop="leaveTime">
          <el-date-picker clearable
                          v-model="form.leaveTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          @change="getDay"
                          placeholder="请选择离店时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getRoomList">获取空闲房间</el-button>
        </el-form-item>
        <el-form-item label="房间" prop="roomId">
          <el-select v-model="form.roomId" placeholder="请选择房间" >
            <el-option
              v-for="item in roomList"
              :key="item.id"
              :label="item.name"
              @click.native="getDay"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="居住天数" prop="dwellingDay">
          <el-input v-model="form.dwellingDay" placeholder="请输入居住天数" disabled/>
        </el-form-item>
        <el-form-item label="订单价格" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入订单价格"  disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRoomOrder,
  getRoomOrder,
  audit,
  addRoomOrder,
  updateRoomOrder,
  listFreeRoom
} from "@/api/system/roomOrder";
import {delRestaurantOrder} from "@/api/system/restaurantOrder";

export default {
  name: "RoomOrder",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      roomList:[],
      // 客房订单表格数据
      roomOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        idCard: null,
        phone: null,
        liveTime: null,
        leaveTime: null,
        dwellingDay: null,
        status: null,
        roomId: null,
        orderPrice: null,
        createUser: null,
        updateUser: null,
        isDeleted: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "预订人姓名不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号码不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
        liveTime: [
          { required: true, message: "入住时间不能为空", trigger: "blur" }
        ],
        leaveTime: [
          { required: true, message: "离店时间不能为空", trigger: "blur" }
        ],
        dwellingDay: [
          { required: true, message: "居住天数不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "订单状态不能为空", trigger: "change" }
        ],
        roomId: [
          { required: true, message: "房间id不能为空", trigger: "change" }
        ],
        orderPrice: [
          { required: true, message: "订单价格不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客房 */
    getRoomList() {
      listFreeRoom(this.form.liveTime,this.form.leaveTime,this.form.id).then(response => {
        this.roomList = response.data;
      });
    },
    getDay(){
      let leave = this.form.leaveTime;
      let live = this.form.liveTime;
      if (live!=null&&leave!=null){
        let leaveDay = new Date(leave);
        let liveDay = new Date(live);
        let differDay = Math.round((leaveDay-liveDay)/1000/60/60/24);
        this.form.dwellingDay = differDay;
        let arr = this.roomList.filter(item=>item.id == this.form.roomId);
        this.form.orderPrice = (differDay * new Number(arr[0].roomPrice)).toString();
        console.log(differDay,this.form)
        this.$forceUpdate()
      }
    },
    /** 查询客房订单列表 */
    getList() {
      this.loading = true;
      listRoomOrder(this.queryParams).then(response => {
        this.roomOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userName: null,
        idCard: null,
        phone: null,
        liveTime: null,
        leaveTime: null,
        dwellingDay: null,
        status: null,
        roomId: null,
        orderPrice: null,
        createTime: null,
        createUser: null,
        updateTime: null,
        updateUser: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客房订单";
    },
    handleAudit(row){
      const id = row.id
      this.$modal.confirm('是否确认将订单状态改为已安排').then(function() {
        return audit(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("审核成功");
      }).catch(() => {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRoomOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客房订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.getDay()
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRoomOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoomOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
